package com.eliza.base.views.learn.text

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController

object UserInputX {
    @Composable
    fun Show(navController: NavController) {
        Column {
            var count by remember {
                mutableIntStateOf(0)
            }
            Button(onClick = { count = (count + 1) % 2 }) {
                Text(text = "UserInputX $count")
            }
            when (count) {
                0 -> FilledTextFieldX()
                1 -> AnnotatedClickableText()

            }
        }

    }

    @Composable
    private fun FilledTextFieldX() {
        var text by remember { mutableStateOf("可輸入框") }
        TextField(
            value = text,
            onValueChange = { text = it },
            label = { Text("Enter Account") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text)
        )
        var password by rememberSaveable { mutableStateOf("") }
        TextField(
            value = password,
            onValueChange = { password = it },
            label = { Text("Enter password") },
            //VisualTransformation，例如将密码中的字符替换为 *，
            visualTransformation = PasswordVisualTransformation(),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
        )
//    请在编写任何转换逻辑时假设传递到 onValueChange 的当前文本与将传递给 onValueChange 的上一个或下一个值无关。
        var input by rememberSaveable { mutableStateOf("") }
        TextField(
            value = input,
            label = { Text("trimStart 0")},
            onValueChange = { newText ->
                input = newText.trimStart { it == '0' }
            }
        )
    }

    @Composable
    private fun AnnotatedClickableText() {
        val annotatedText = buildAnnotatedString {
            append("Click ")
            pushStringAnnotation(
                tag = "URL",
                annotation = "https://developer.android.com"
            )
            withStyle(
                style = SpanStyle(
                    color = Color.Blue,
                    fontWeight = FontWeight.Bold
                )
            ) {
                append("here")
            }
            pop()
        }
        ClickableText(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.Gray),
            text = annotatedText,
            style = TextStyle(fontSize = 20.sp),
            onClick = { offset ->
                annotatedText.getStringAnnotations(
                    tag = "URL", start = offset,
                    end = offset
                )
                    .firstOrNull()?.let { annotation ->
                        // If yes, we log its value
                        Log.d("Clicked URL", annotation.item)
                    }
            }
        )
    }

}

object UserInteractionsX {
    @Composable
    fun Show(navController: NavController) {
        Column {
            var count by remember {
                mutableIntStateOf(0)
            }
            Button(onClick = { count = (count + 1) % 4 }) {
                Text(text = "PointerX $count")
            }
            when (count) {
                1 -> Selection()

            }
        }

    }

    @Composable
    private fun Selection() {
        Column {
            SelectionContainer {
                Text("可以選中")
            }
            var position by remember { mutableStateOf("Hello") }
            DisableSelection {
                Text(
                    "DisableSelection: $position",
                    modifier = Modifier
                        .background(Color.Red)
                        .alpha(0.5F)
                )
            }
            //获取点击文字的位置，下標
            ClickableText(
                text = AnnotatedString(position),
                onClick = { offset ->
                    position += offset.toString()
                },
                style = TextStyle(fontSize = 30.sp),
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.Cyan)
            )
            //
            val annotatedText = buildAnnotatedString {
                append("Click ")
                // We attach this *URL* annotation to the following content
                // until `pop()` is called
                pushStringAnnotation(
                    tag = "URL", annotation = "https://developer.android.com"
                )
                withStyle(
                    style = SpanStyle(
                        color = Color.Blue, fontWeight = FontWeight.Bold
                    )
                ) {
                    append("here")
                }
                pop()
            }

            ClickableText(text = annotatedText, onClick = { offset ->
                // We check if there is an *URL* annotation attached to the text
                // at the clicked position
                annotatedText.getStringAnnotations(
                    tag = "URL", start = offset, end = offset
                ).firstOrNull()?.let { annotation ->
                    // If yes, we log its value
                    Log.d("Clicked URL", annotation.item)
                }
            })
        }
    }

}